package cn.iocoder.yudao.module.nmkj.controller.app.tag;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.nmkj.controller.admin.tag.vo.TagPageReqVO;
import cn.iocoder.yudao.module.nmkj.controller.admin.tag.vo.TagRespVO;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.tag.TagDO;
import cn.iocoder.yudao.module.nmkj.service.tag.TagService;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.yudao.module.system.service.dict.DictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户APP - 标签管理")
@RestController
@RequestMapping("/nmkj/tag")
@Validated
public class AppTagController {

    @Resource
    private TagService tagService;

    @Resource
    private DictDataService dictDataService;

    private static final String  DICT_TAG = "nmkj_type_tag";

    @GetMapping("/lists")
    @Operation(summary = "获得标签管理列表分组")
    public CommonResult<PageResult<TagRespVO>>[] getTagPage(@Valid TagPageReqVO reqVO) {
        reqVO.setStatus(1); //用户端只允许获取启用状态
        //系统的启用状态为 0, 1为关闭
        List<DictDataDO> dictList = dictDataService.getDictDataList(0, DICT_TAG);
        CommonResult<PageResult<TagRespVO>>[] results = new CommonResult[dictList.size()];
        //对类型分类查询
        for (int i = 0; i < dictList.size(); ++i) {
            DictDataDO dictData = dictList.get(i);
            reqVO.setType(Long.parseLong(dictData.getValue()));
            List<TagDO> tagDOList = tagService.getTagList(reqVO);
            PageResult<TagDO> pageResult = new PageResult<>(tagDOList, (long) tagDOList.size());
            results[i] = success(BeanUtils.toBean(pageResult, TagRespVO.class));
            Map<String, Object> map = new HashMap<>();
            map.put("label", dictData.getLabel());
            pageResult.put(map);
            results[i].getData().put(map);
        }
        return results;
    }

}
